const express=require("express")
const router=express.Router();
const pool=require("../pool");

router.get("/info",(req,res)=>{
  //创建一个接收数据的对象
  var carInfoData={
    carInfo:{},
    carType:{},
    carStages:{},
    carNews:{}
  };
  var id=req.query.did;
  new Promise((resolve,reject)=>{
    var sql=`select
             did,src,title,cartype
             FROM car_details_info 
             WHERE did=?`;
    pool.query(sql,[id],(err,result)=>{
      if(err) console.log(err);
      carInfoData.carInfo=result;
      resolve(true);
    })
  }).then(value=>{
    return new Promise((resolve,reject)=>{
      //从car_details_info表中获得于car_details_type表关联的cartype
      var typeid=carInfoData.carInfo[0].cartype;
      console.log(typeid);
      var sql=`select
               did,typename,typeid,stageid,capacity,voyage,
               fastchange,slowchange,handsel,dpayment,fpayment,
               oprice,aprice
               FROM car_details_type
               WHERE typeid= ? `;
      pool.query(sql,[typeid],(err,result)=>{
        if(err) console.log(err);
        carInfoData.carType=result;
        resolve(true);
      })
    }).then(value=>{
      return new Promise((resolve,reject)=>{
        //查询汽车分期期数
        var stageId=carInfoData.carType[0].stageid;
        var sql=`select did,stanum,stage FROM car_details_stages
                 WHERE stanum=?`;
        pool.query(sql,[stageId],(err,result)=>{
          if(err) console.log(err);
          carInfoData.carStages=result;
          //res.send({code:1,data:carInfoData});
          //res.end();
          resolve(true);
        })
      })
    }).then(value=>{
      return new Promise((resolve,reject)=>{
        //查询文章列表
        //1:参数 pno pageSize
        var pno=req.query.pno;
        var pageSize=req.query.pageSize;
            //2:允许使用默认值  1 7
        if(!pno){pno=1}
        if(!pageSize){pageSize=7}

        var sql =" SELECT did,title,comments";
            sql+=" dates,pic";
            sql+=" FROM yhc_discover_all";
            sql+=" LIMIT ?,?";
        var ps=parseInt(pageSize);
        var offset=(pno-1)*ps;

        pool.query(sql,[offset,ps],(err,result)=>{
          if(err) console.log(err);
          carInfoData.carNews=result;
          res.send({code:1,data:carInfoData});
          res.end();
        })
      })
    })
  })
})
router.get("/vedios",(req,res)=>{
  console.log(77);
  //创建一个接收数据的对象
  let id=req.query.did;
  //查询视频列表
  let pno=req.query.pno;
  let pageSize=req.query.pageSize;
      //2:允许使用默认值  1 7
      if(!pno){pno=1};
      if(!pageSize){pageSize=3};
  let sql =" SELECT did,via,author,";
      sql+=" dates,title,";
      sql+=" pic,share,";
      sql+=" comments,likes";
      sql+=" FROM yhc_videoList";
      sql+=" LIMIT ?,?";
  let ps=parseInt(pageSize);
  let offset=(pno-1)*ps;
      pool.query(sql,[offset,ps],(err,result)=>{
        if(err) console.log(err);
        console.log(result);
        res.send({code:1,data:result})
      })
})
module.exports=router;